#include"../../comm/detail.hpp"
#include"../../server/rpc_server.hpp"

using namespace std;
using namespace MuduoNet;
using namespace Message;
using namespace Dispatch;
using namespace Server;

// void OnRpcRequest(const BaseConnection::ptr &conn,  RpcRequest::ptr &msg)
// {
//     cout << "收到rpc请求"<<msg->Method()<<endl;
//     string bady = msg->Serialize();
//     cout << bady << endl;
//     auto rpc_rsp = MessageFactory::create<RpcResponse>();
//     rpc_rsp->SetId("111");
//     rpc_rsp->SetMtype(Mtype::RSP_RPC);
//     rpc_rsp->SetRcode(RCode::RCODE_OK);
//     rpc_rsp->SetResult(33);
//     conn->Send(rpc_rsp);
// }
// void OnTopicRequest(const BaseConnection::ptr &conn, TopicRequest::ptr &msg)
// {
//     cout << "收到Topic请求" << endl;
//     string bady = msg->Serialize();
//     cout << bady << endl;
//     auto topic_rsp = MessageFactory::create<TopicResponse>();
//     topic_rsp->SetId("22222");
//     topic_rsp->SetMtype(Mtype::RSP_TOPIC);
//     topic_rsp->SetRcode(RCode::RCODE_OK);
//     conn->Send(topic_rsp);
// }

void Add(const Json::Value &req, Json::Value &rsp) {
    int num1 = req["num1"].asInt();
    int num2 = req["num2"].asInt();
    rsp = num1 + num2;
    DLog("Add:%d", rsp.asInt());
}
int main()
{
    unique_ptr<Server::SDescribeFactory> desc_factory(new SDescribeFactory());

    desc_factory->setMethodName("Add");
    desc_factory->setParamsDesc("num1",Server::VType::INTEGRAL);
    desc_factory->setParamsDesc("num2",Server::VType::INTEGRAL);
    desc_factory->setReturnType(Server::VType::INTEGRAL);
    desc_factory->setCallback(Add);
    
    Server::RpcServer server(Address("127.0.0.1",9090));
    server.RegistryMethod(desc_factory->build());
    server.Start();
    return 0;
}